﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using NCS.IConnect.ApplicationContexts;
using JM.JCY.Proxy;
using Microsoft.SharePoint;
using JM.JCY.Common;

namespace JM.JCY.SharePointComponent.Department.DepartmentInformation
{
    public partial class DepartmentInformationUserControl : UserControl
    {
        public int _userId
        {
            set { ViewState["_userId"] = value; }
            get { return (int)ViewState["_userId"]; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (ApplicationContext.Current.GetKeyValue("userId") != null)
            {
                _userId = Int32.Parse(ApplicationContext.Current.GetKeyValue("userId").ToString());
            }
           if (Request.QueryString["DeptId"] != null)
                {
                    
                    using (UserManagementProxy proxy = new UserManagementProxy())
                    {
                        var dt = proxy.GetDeptById(Int32.Parse(Request.QueryString["DeptId"].ToString()), string.Empty);

                        if (dt.Rows.Count > 0)
                        {
                            this.lblDeptNameDoc.Text = dt[0].DeptName;
                            BindDeptCultureResposibilityRisk(this.lblDeptNameDoc.Text);
                            if (!IsExistUser(_userId, Int32.Parse(Request.QueryString["DeptId"].ToString())))
                            {
                                //this.tr_DeptDoc.Visible = false;
                                this.imgEdit.Visible = false;
                                this.idHomePageEditItem.Visible = false;
                            }
                            else
                            {
                                //this.tr_DeptDoc.Visible = true;
                                SPWeb myWeb = SPContext.Current.Web;
                                //if (IsExistList(myWeb, this.lblDeptNameDoc.Text))
                                //{
                                //    SPList docLibrary = myWeb.Lists[this.lblDeptNameDoc.Text];
                                //    this.lvDeptDocument.ListId = docLibrary.ID.ToString();
                                //    this.lvDeptDocument.ViewId = docLibrary.DefaultView.ID.ToString();
                                //    string onclick = string.Format("javascript:NewItem2(event, '{0}');javascript:return false;", docLibrary.DefaultNewFormUrl);
                                //    this.idHomePageNewItemDeptDoc.Attributes.Add("onclick", onclick);
                                //}
                                SPItem spItem = GetDeptInfoList(this.lblDeptNameDoc.Text);
                                SPList deptList = myWeb.Lists[JCYConstant.SharePointListName.DepartmentInfoList];
                                string editonclick = string.Format("javascript:NewItem2(event, '{0}');javascript:return false;", deptList.DefaultEditFormUrl + "?ID=" + spItem.ID);
                                this.idHomePageEditItem.Attributes.Add("onclick", editonclick);
                             
                            }
                        }

                    }
                }
        }

        private bool IsExistList(SPWeb web, string listName)
        {
            bool isExist = false;
            SPListCollection lists = web.Lists;
            foreach (SPList item in lists)
            {
                if (item.Title.Equals(listName))
                {
                    isExist = true;
                    break;
                }

            }

            return isExist;
        }

        public bool IsExistUser(int userId, int deptId)
        {
            try
            {
                using (UserManagementProxy proxy = new UserManagementProxy())
                {
                    var dt = proxy.GetUserDeptMatch(userId, deptId);
                    if (dt.Rows.Count > 0)
                        return true;
                    else
                        return false;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void BindDeptCultureResposibilityRisk(string deptName)
        {
            SPListItem spItem = GetDeptInfoList(deptName);
           
           this.lbDeptDesc.Text = spItem["部门描述"].ToString();
           this.lbDeptCulture.Text = spItem["部门文化"].ToString();
           this.lbDeptResposibility.Text = spItem["部门职责"].ToString();
           this.lbDeptRisk.Text  = spItem["主要廉政风险点"].ToString();
           this.lbDeptPrevention.Text  = spItem["对应防控措施"].ToString();
           
        }

        private static SPListItem GetDeptInfoList(string deptName)
        {
            string listName = JCYConstant.SharePointListName.DepartmentInfoList;
            SPWeb myWeb = SPContext.Current.Web;
            SPList DeptInfoList = myWeb.Lists[listName];
            SPQuery query = new SPQuery();
            query.Query = @"<Where>
            <Eq>
            <FieldRef Name='Title'/>
            <Value Type='Text'>" + deptName + @"</ValueType>
            </Eq>
            </Where>";
            SPListItem spItem = DeptInfoList.GetItems(query)[0];
            return spItem;
        }
    }
}
